﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace VSTools
{
	public partial class FindFileForm : Form
	{
		public delegate void ResultHandler(DialogResult Dialogresult, string FileName);
		private static FindFileForm _instance = null;
		private List<string> _fileList;
		private string _fileName;
		private ResultHandler _handler;

		public FindFileForm()
		{
			InitializeComponent();
		}

		static FindFileForm()
		{
			_instance = new FindFileForm();
		}

		public static void Show(ResultHandler Handler, string[] FileList)
		{
			_instance.FileList = FileList;
			//Wnd w = new Wnd(hWnd);
			_instance.Show(Handler);
			//_instance.Activate();
			//return _cancel ? DialogResult.Cancel : DialogResult.OK;
		}

		private new void Show(ResultHandler Handler)
		{
			_handler = Handler;
			Show();
			Activate();
			
			ShowFiles();
		}

		/*public static string FileName
		{
			get
			{
				return _instance._fileName;
			}
		}*/

		public string[] FileList
		{
			set
			{
				_fileList = new List<string>(value);
			}
		}

		private void tFile_KeyUp(object sender, KeyEventArgs e)
		{
			switch (e.KeyCode)
			{ 
				case Keys.Enter:
					e.Handled = true;
					OK();
					break;

				case Keys.Escape:

					Cancel();
					break;

				case Keys.Down:

					//MoveDown();
					break;

				case Keys.Up:

					//MoveUp();
					break;
			}
		}

		private void MoveDown()
		{
			if (listBox1.Items.Count > listBox1.SelectedIndex + 1)
				listBox1.SelectedIndex++;
		}

		private void MoveUp()
		{
			if (listBox1.SelectedIndex > 0)
				listBox1.SelectedIndex--;
		}

		private void Cancel()
		{
			DialogResult = DialogResult.Cancel;
			ReturnResult();
		}

		private void OK()
		{
			_fileName = listBox1.SelectedItem.ToString();
			DialogResult = DialogResult.OK;
			ReturnResult();
		}

		private void ReturnResult()
		{
			_handler.Invoke(DialogResult, _fileName);
			Hide();
		}

		private void tFile_TextChanged(object sender, System.EventArgs e)
		{
			ShowFiles();
		}

		private void ShowFiles()
		{
			listBox1.Items.Clear();
			List<string> matches = _fileList.FindAll(Find);
			listBox1.Items.AddRange(matches.ToArray());
			int h = (listBox1.ItemHeight * listBox1.Items.Count) + listBox1.Top;

			if (Top + h > Screen.PrimaryScreen.WorkingArea.Height - 30)
			{
				h = Screen.PrimaryScreen.WorkingArea.Height - Top - 30;
			}

			Height = h + 2;

			if (listBox1.Items.Count > 0)
				listBox1.SelectedIndex = 0;
		}

		private bool Find(string Match)
		{
			bool res =Regex.IsMatch(Match, @"^" + Regex.Escape(tFile.Text), RegexOptions.IgnoreCase);
			return res;
		}

		private void listBox1_DoubleClick(object sender, System.EventArgs e)
		{
			//OK();
		}

		private void listBox1_SelectedValueChanged(object sender, System.EventArgs e)
		{

		}

		private void tFile_KeyDown(object sender, KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.Enter:

					e.Handled = true;
					break;

				case Keys.Down:

					MoveDown();
					break;

				case Keys.Up:

					MoveUp();
					break;
			}
		}

		private void listBox1_Click(object sender, System.EventArgs e)
		{
			OK();
		}

		private void FindFileForm_Activated(object sender, System.EventArgs e)
		{
			tFile.Focus();
			tFile.SelectAll();
		}

		protected override void OnDeactivate(System.EventArgs e)
		{
			base.OnDeactivate(e);

			Cancel();
		}
	}
}
